package com.axonactive.yolo.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class Utils {
	private static final Logger LOGGER = Logger.getLogger(Utils.class);
	private static Pattern pattern;
	private static Matcher matcher;

	public static final SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy/MM/dd");
	
	public static final String[] cabinClasses = { "Economy", "Business", "All" };
	
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	private static final String NAME_PATTERN = "[a-zA-Z.? ]*";

	public static boolean isValidPassword(String pass) {
		if (pass.length() < 6)
			return false;
		return true;
	}

	public static boolean isValidEmail(String pass) {
		if (pass.length() >= 256 || pass == "") {
			return false;
		}
		pattern = Pattern.compile(EMAIL_PATTERN);
		matcher = pattern.matcher(pass);
		return matcher.matches();
	}

	public static boolean isValidPhoneNUmber(String str) {
		str = str.replaceAll(" ", "");
		if (str.length() <= 3) {
			return false;
		}
		return str.matches("[+-]?\\d*(\\.\\d+)?");
	}

	public static int convertToInt(String numStops) {
		try {
			return Integer.valueOf(numStops);
		} catch (NumberFormatException e) {
			LOGGER.error("Utils class convertToInt error", e);
			return 0;
		}
	}
	
	public static double convertToDouble(String inputString) {
		try {
			return Double.valueOf(inputString);
		} catch (NumberFormatException e) {
			LOGGER.error("Utils class convertToDouble error", e);
			return 0;
		}
	}

	public static String convertToString(int numStops) {
		try {
			return String.valueOf(numStops);
		} catch (NumberFormatException e) {
			LOGGER.error("Utils class convertToString error", e);
			return "0";
		}
	}

	public static String getURLWithContextPath(HttpServletRequest request) {
		return request.getScheme() + "://" + request.getServerName() + ":"
				+ request.getServerPort() + request.getContextPath();
	}

	public static boolean isValidName(String str) {
		if (str.length() > 50)
			return false;
		else {
			pattern = Pattern.compile(NAME_PATTERN);
			matcher = pattern.matcher(str);
			return matcher.matches();
		}
	}

	public static void main(String[] args) {
		LOGGER.info(isValidName("Trinh"));
	}

	public static String formatNumber(int number) {
		try {
			NumberFormat formatter = new DecimalFormat("00");
			String thang = formatter.format(number);
			return thang;
		} catch (Exception e) {
			Logger.getLogger(Utils.class).info("format number exception", e);
		}
		return "" + number;
	}

	public static int getDaysBetweenTwoDate(Date date1, Date date2) {
		return (int) ((date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24l));
	}

	public int convertTimeByPerFiveMinutes(String[] durationTime) {
		int hour = Integer.parseInt(durationTime[0]);
		int minute = Integer.parseInt(durationTime[1]);
		int timePerFiveMinutes = (hour * CONSTANTS.HOUR)
				+ (minute / CONSTANTS.MINUTE);
		return timePerFiveMinutes;
	}

	public static int hasContain(String[] subject,String element){
		for (int i = 0; i < subject.length; i++) {
			if (subject[i].equals(element))
				return i;
		}
		return -1;
	}

	public static String roundNDigit(double d, int n){
		double x=Math.pow(10, n);
		return Math.round(d*x)/x+"";
	}
	
	public static String convertCabinClass(String cabinClass){
		if(CONSTANTS.CABIN_ECONOMY_STRING.equals(cabinClass)) return CONSTANTS.CABIN_ECONOMY_0;
		if(CONSTANTS.CABIN_BUSSINESS_STRING.equals(cabinClass)) return CONSTANTS.CABIN_BUSSINESS_1;
		return CONSTANTS.CABIN_ALL_2;
	}
}

